import Vue from 'vue'
import Home from '../views/Home.vue'
import VueRouter from 'vue-router'
import Router from 'vue-router';
//路由导航冗余报错（路由重复）
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/expressage',
    name: 'Expressage',
    component: () => import('../views/Expressage.vue'),
  },
  {
    path: '/staff',
    name: 'Staff',
    component: () => import('../views/Staff.vue'),
  },
  {
    path: '/money',
    name: 'Money',
    component: () => import('../views/Money.vue'),
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('../views/User.vue'),
  },
  {
    path: '/news',
    name: 'News',
    component: () => import('../views/News.vue'),
  },
  {
    path: '/index',
    name: 'Index',
    meta: {
      requiresAuth: true
    },
    component: () => import('../views/Index.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    meta: {
      requiresAuth: true
    },
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/repair',
    name: 'Repair',
    component: () => import('../views/Repair.vue'),
  },
]

const router = new VueRouter({
  routes
})


// 路由守卫
router.beforeEach((to, from, next) => {
  var userInfo = localStorage.getItem("communityUser");
  if (userInfo) {
    next()
  } else {
    if (to.path == "/login") {
      next()
      return
    }
    next({
      path: "/login"
    })
  }
})

export default router